package mystia.lorelei.app.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import mystia.lorelei.app.entity.Employee;
import mystia.lorelei.app.service.EmployeeService;
import mystia.lorelei.app.util.Result;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Tag(name = "员工管理")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @Operation(summary = "员工列表",description = "多条件查询员工列表")
    @GetMapping("/list/condition")
    public Result<Page<Employee>> listEmployeesByCondition(@RequestParam(required = false) String sex,
                                                           @RequestParam(required = false) String job,
                                                           @RequestParam(required = false) Boolean exited,
                                                           @RequestParam Integer page,
                                                           @RequestParam Integer size) {
        return Result.ok(employeeService.listEmployeesByPage(sex,job,exited, page, size));
    }
    
    @Operation(summary = "批量获取",description = "可用于导出")
    @GetMapping("/list")
    public Result<List<Employee>> listEmployee() {
        return Result.ok(employeeService.listEmployee());
    }
    
    @PostMapping("/save")
    @Operation(summary = "保存/更新员工")
    public Result saveEmployee(@RequestBody Employee employee){
        employeeService.saveEmployee(employee);
        return Result.ok();
    }
    
    @GetMapping("/delete/{id}")
    @Operation(summary = "删除员工")
    public Result deleteEmployee(@PathVariable("id")int id){
        employeeService.deleteEmployee(id);
        return Result.ok();
    }
    
    @GetMapping("/delete")
    @Operation(summary = "批量删除")
    public Result deleteEmployees(@RequestBody List<Integer> ids){
        try{
            employeeService.deleteEmployees(ids);
            return Result.ok();
        }catch (Exception e){
            return Result.fail();
        }
    }
    
    @PutMapping("/update/exited")
    @Operation(summary = "在职状态",description = "修改员工在职状态")
    public Result updateExited(@RequestParam int id,
                               @RequestParam Date date,
                               @RequestParam boolean exited){
        employeeService.updateExited(exited,date,id);
        return Result.ok();
    }
}
